package com.hyj.ecp.backstage.order.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.ecp.ResultObject;
import com.ecp.order.OrderQO;
import com.hyj.ecp.backstage.order.service.IBackOrderService;
import com.hyj.ecp.common.dao.IjdbcDAO;

@Service
public class BackOrderService implements IBackOrderService{

Log logger = LogFactory.getLog(BackOrderService.class);
	
	@Resource(name="jdbcDao")
    private  IjdbcDAO dao;
	
	@Override
	public Map<String, Object> queryOrder(OrderQO qo) {
		List<Object> param = new ArrayList<Object>();
		String sql = " select t.id,u.account as account,DATE_FORMAT(t.addtime,'%Y-%m-%d %H-%i-%S') as addtime,t.price,t.toid " +
				" ,(select s.value from t_sys_config s where s.TYPE='orderstatus' and s.key=t.status) as status,t.type    " +
				"  from t_order_info t, t_client_user u where t.account = u.account and t.type= ?  ";
		param.add(qo.getType());
		if(qo.getCustId()!=0){
			sql= sql+" and t.custId = ? ";
			param.add(qo.getCustId());
		}
		
		sql = sql+" order by t.id desc ";
		ResultObject result = dao.pageQuery(sql, qo.getCurPage(), qo.getSize(),  param.toArray());
		Map<String,Object> vo = new HashMap<String,Object>();
    	vo.put("rowTotal", result.getRowCount());
    	vo.put("infoContent", result.getRows());
		return  vo;
	}

	@Override
	public List<Map<String,Object>> queryOrderInfo(OrderQO qo) {
      String sql = "  select t.account,t.price,t.addtime," +
   " (SELECT CONCAT('收件人:',i.toname,' 地址：',i.toaddr,' 联系电话:',i.tomobile,' 邮编：',CAST(i.postcode  AS  CHAR(7))) " +
		        "  FROM t_client_toinfo i WHERE i.id=t.TOID) AS addr  "+
				",t.status,t1.goodid,t1.goodname ,t1.price ,t1.num,t1.point " +
				"     from t_order_info t,t_order_info_goods t1  where t.id= t1.orderid " +
				"   and t.id =? ";
		
		
		return dao.queryForList(sql,new Object[]{qo.getId()});
	}

	@Override
	public int updateOrderInfo(OrderQO qo) {
		String sql = "   UPDATE t_order_info SET STATUS=? WHERE id= ?  ";
		
		return dao.update(sql, new Object[]{qo.getStatus(),qo.getId()});
	}

}
